package top.itaso.app.ui.apps

import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import top.itaso.lib.widget.tpv.TpvCommonItemView

/**
 * @Description:
 * @Author itsdf07
 * @E-Mail 923255742@qq.com
 * @Github https://github.com/itsdf07
 * @Date 2021/6/20
 */
class AppsAdapter(
    private var mAppData: List<AppEntity>,
    private var onItemClickListener: IOnItemClickListener
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    //定义接口
    interface IOnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        Log.d(TAG, "onCreateViewHolder: >>>>>>viewType=$viewType")
        val layoutItemApp = TpvCommonItemView(parent.context)
        layoutItemApp.setLayoutVisibility(63)
        return ViewHolder(layoutItemApp)
    }

    //    override fun getItemCount(): Int = datas.size
    override fun getItemCount(): Int {
        return mAppData.size
    }

    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        val appInfo = mAppData[position]

        val layoutItemApp = holder.itemView as TpvCommonItemView
        layoutItemApp.setIconImageDrawable(mAppData[position].appIcon)
        layoutItemApp.titleContent = appInfo.appName
        layoutItemApp.setSubTitleContent("${appInfo.appVersionName}-${appInfo.appVersionCode}")
        layoutItemApp.setOtherContent(appInfo.appPackageName)
        layoutItemApp.setMarkContent(if (appInfo.isSystemApp) "预装" else "后装")
        Log.v(TAG, "onBindViewHolder: >>>>>>appInfo${appInfo.toString()}")
        layoutItemApp.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position)
                }
            }

        })
    }

    fun update(datas: List<AppEntity>) {
        this.mAppData = datas
        notifyDataSetChanged()
    }

    internal class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(
        itemView!!
    )

    companion object {
        private const val TAG = "AppsAdapter"
    }
}